package com.xisan.practice.rabbitmq.workqueue;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import com.xisan.practice.rabbitmq.utils.RabbitMQUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * work模型--每个消费者都平均消费，消费慢的也会逐渐消费，即使其它效率快的也不会多劳多得
 * 方案：每次消费1条、并且手动确认--解决能者多劳
 */
public class Provider {

    @Test
    public void sendMessage() throws IOException {

        //1.工具类获取连接对象
        Connection connection = RabbitMQUtils.getConnection();
        //2.创建通道
        assert connection != null;
        Channel channel = connection.createChannel();
        //3.通道绑定队列
        //队列、是否持久化、是否独占、是否自动删除(当消费者不占用队列后才会删除)、附加信息
        channel.queueDeclare("work", false, false, false, null);
        //4.发布消息
        //交换机、队列、传递消息额外设置(消息持久化)、消息的内容
        for (int i = 0; i < 20; i++) {
            // 生成20条消息，供多个消费者消费
            channel.basicPublish("", "work", null, "hello work".getBytes());
        }
        //5.关闭
        RabbitMQUtils.close(channel, connection);
    }
}
